package CompressedFileSplitAndMerge;

import java.io.*;

public class Split {
    public static void main(String[] args) {
//        File file = new File("d:/img/tupian.zip");
//        System.out.println(file.getPath());
//        FileInputStream fis = null;
//        FileOutputStream fos =null;
//        try {
//            fis = new FileInputStream(file);
//            byte[] buffer = new byte[1024*10];
//            int index = 0;
//            int count = -1;
//            while ((count = fis.read(buffer)) != -1){
//                fos = new FileOutputStream( file.getPath() + "_" + (++index));
//                fos.write(buffer,0,count);
//                fos.flush();
//            }
//            System.out.println("拆分成功，共拆分为：" + index + "个");
//
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }finally {
//            if (fis!=null){
//                try {
//                    fis.close();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//            if(fos!=null){
//                try {
//                    fos.close();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//
//        }
        File file2 = new File("d:/img2/tupian.zip_1");
        FileInputStream fisZip = null;
        FileOutputStream fosZip = null;
        try {
            String path = (file2.getPath()).substring(0, (file2.getPath()).indexOf("_"));
            fosZip = new FileOutputStream(path);
            int index = 0;
            while (new File(path + "_" + (++index)).exists()) {
                fisZip = new FileInputStream(path + "_" + index);
                byte[] buffer = new byte[fisZip.available()];
                fisZip.read(buffer);
                fosZip.write(buffer);
                fosZip.flush();
                fisZip.close();
            }
            System.out.println("文件合并成功");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fosZip != null){
                try {
                    fosZip.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
